package com.sendi.system.bean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;

import javax.sql.DataSource;

import org.apache.struts2.components.If;

import com.sendi.system.entity.SysHostPower;
import com.sendi.system.entity.SysHostTree;
import com.sendi.system.entity.SystemPower;
import com.sendi.system.entity.User;
import com.sendi.system.entity.UserRoleRelation;
import com.sendi.system.service.ServiceSupportImp;
import com.sendi.system.util.tree.HostTreeNode;
import com.sendi.system.util.tree.TreeNode;

/*
 * 系统设备权限工具
 *  
 */
public class SysHostPowerHelper {
	  public DataSource dataSource;
	  public ApplicationContextHelper applicationContextHelper;
	  public ServiceSupportImp serviceSupportImp;
 
	    private HashMap<String,UserRoleRelation> userRoleRelation=null;
	    private HashMap<String,SysHostPower> systemhostPower=null;
	    private HashMap<String,SysHostTree> systemhostInf=null;
	    private HashMap<String,HostTreeNode> systemhosttreemap=null;
	    
	  /*
	   * 初始化引用数据
	   */
	  public void initRefData()
	  {
		  try
		  {
			  userRoleRelation=(HashMap<String,UserRoleRelation> )this.applicationContextHelper.servletContext.getAttribute("sendi_sys_userRoleRelation_info");
			  systemhostPower=(HashMap<String,SysHostPower> )this.applicationContextHelper.servletContext.getAttribute("sendi_sys_hostPower_info");
			  systemhostInf=( HashMap<String,SysHostTree>)this.applicationContextHelper.servletContext.getAttribute("sendi_sys_hosttree_info");
			  systemhosttreemap=( HashMap<String,HostTreeNode>)this.applicationContextHelper.servletContext.getAttribute("sendi_sys_host_tree");
	 	  }
		  catch (Exception e) {
			e.printStackTrace();
		}
		  
	  }
	/*
	 * 根据用户ID，跟设备ID判断该用户是否具有该访问权限
	 * 
	 */
	public boolean judeuserHostPower(String userid,String hostid)
	{
		  ArrayList<String>  powers=queryUserHostPower(userid,"-1");
		 for(String a:powers)
		 {
			 if(a.equals(hostid))
			 {
				 return true;
			 }
			 
		 }
		return false;
		
    }
	
	/*
	 * 根据角色ID，跟设备ID判断该角色是否具有该访问权限
	 */
	public boolean juderoleHostPower(String roleid,String hostid)
	{
		 ArrayList<String>  powers=queryRolerHostPower(roleid,"-1");
		 for(String a:powers)
		 {
			 if(a.equals(hostid))
			 {
				 return true;
			 }
			 
		 }
		return false;
	 
    }
	/*
	 * 查询该用户所有的设备权限，注意不是设备树中的树ID，而是ishost为y的host_id
	 * userid 是用户表中主键ID
	 * 
	 */
	public ArrayList<String> queryUserHostPower(String userid)
	{
	 	return queryUserHostPower(userid,"-1");
	}
	
	/*
	 * 查询该角色所有的设备权限，注意不是设备树中的树ID，而是ishost为y的host_id
	 * userid 是用户表中主键ID
	 */
	public ArrayList<String> queryRoleHostPower(String roldeid)
	{
 		return queryRolerHostPower(roldeid,"-1");
	}
	
	
	
	/*
	 * 查询该用户所有的设备权限，注意不是设备树中的树ID，而是ishost为y的host_id
	 * userid 是用户表中主键ID
	 * hosttreeid sys_host_tree 中的id值 
	 */
	public ArrayList<String> queryUserHostPower(String userid,String hosttreeid)
	{
	 	ArrayList<String> power=new ArrayList<String>();
	 	HashSet<String> roleids=selectUserroles(userid);
	 	
	 	HashMap<String, SysHostTree> result=new HashMap<String, SysHostTree>();
	 	IteratorHostMap(result,hosttreeid); //递归出来的都是设备的
	 	
	 	Collection<SysHostPower> c=this.systemhostPower.values();
	 	for(SysHostPower s:c)
	 	{
	 		SysHostTree sht=result.get(s.getHostreeId().toString());
	 		if(sht!=null) //说明是设备节点
	 		{
	 			if(roleids.contains(s.getRoleid().toString()))
	 			{
	 				
	 				power.add(sht.getHostId().toString());
	 			}
	 			
	 		}
	 		
	 	}
	 	
		return power;
		
	}
	/*
	 * 根据hosttreeid 递归出所有的设备的map
	 */
	public void IteratorHostMap(HashMap<String, SysHostTree> result,String hosttreeid)
	{
	 	HostTreeNode re=systemhosttreemap.get(hosttreeid);
	 	if(re.isleaf)
	 	{
	 		result.put(String.valueOf(re.getTreeId()), re.fn);
	  	}
	 	else
	 	{
	 		ArrayList<TreeNode> child =re.child;
	 		for(TreeNode tn:child)
	 		{
	 			IteratorHostMap(result,String.valueOf(tn.getTreeId()));
	 		}
	 	}
	 	
	 	
	}
	/*
	 * 查询该用户所有的设备权限，注意不是设备树中的树ID，而是ishost为y的host_id
	 * userid 是用户表中主键ID
	 * hosttreeid sys_host_tree 中的id值 
	 */
	public ArrayList<String> queryRolerHostPower(String roleid,String hosttreeid)
	{
		
		ArrayList<String> power=new ArrayList<String>();
		
		HashMap<String, SysHostTree> result=new HashMap<String, SysHostTree>();
	 	IteratorHostMap(result,hosttreeid); //递归出来的都是设备的
	 	
	 	Collection<SysHostPower> c=this.systemhostPower.values();
	 	for(SysHostPower s:c)
	 	{
	 		SysHostTree sht=result.get(s.getHostreeId().toString());
	 		if(sht!=null) //说明是设备节点
	 		{
	 			if(roleid.equals(s.getRoleid().toString()))
	 			{
	 		 		power.add(sht.getHostId().toString());
	 			}
	 			
	 		}
	 		
	 	}
	 	
		return power;
		
	} 
	
	/*
	 * 查询用户所有的角色
	 */
	public HashSet<String> selectUserroles(String userid)
	{
		HashSet<String> rolids=new HashSet<String>();
		Collection<UserRoleRelation> c=this.userRoleRelation.values();
		if(c!=null)
		{
			for(UserRoleRelation rrr:c)
			{
				if(rrr.getUserid().toString().equals(userid))
				{
					rolids.add(rrr.getRoleid().toString());
				}
				
			}
			
		}
		return rolids;
		
	}
	public DataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public ApplicationContextHelper getApplicationContextHelper() {
		return applicationContextHelper;
	}
	public void setApplicationContextHelper(
			ApplicationContextHelper applicationContextHelper) {
		this.applicationContextHelper = applicationContextHelper;
	}
	public ServiceSupportImp getServiceSupportImp() {
		return serviceSupportImp;
	}
	public void setServiceSupportImp(ServiceSupportImp serviceSupportImp) {
		this.serviceSupportImp = serviceSupportImp;
	}

}
